package cn.foodProject.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import cn.foodProject.R;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.FoodGsonBean;

public class FoodListAdapter extends BaseAdapter {

    private List<FoodGsonBean.Food> objects = new ArrayList<FoodGsonBean.Food>();

    private Context context;
    private LayoutInflater layoutInflater;

    public FoodListAdapter(Context context, List<FoodGsonBean.Food> objects) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public FoodGsonBean.Food getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_list_food, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((FoodGsonBean.Food) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(FoodGsonBean.Food object, ViewHolder holder) {
        Picasso.with(context)
                .load(MyUrl.MY_URL + "/" + object.getIcon())
                .into(holder.imgFoodlist);
        holder.tvFoodlistName.setText(object.getName());
        holder.tvFoodlistLabel.setText(object.getLabel());
        holder.tvFoodlistPrice.setText(object.getPrice()+"");
    }

    protected class ViewHolder {
        private ImageView imgFoodlist;
        private TextView tvFoodlistName;
        private TextView tvFoodlistLabel;
        private TextView tvFoodlistPrice;

        public ViewHolder(View view) {
            imgFoodlist = (ImageView) view.findViewById(R.id.img_foodlist);
            tvFoodlistName = (TextView) view.findViewById(R.id.tv_foodlist_name);
            tvFoodlistLabel = (TextView) view.findViewById(R.id.tv_foodlist_label);
            tvFoodlistPrice = (TextView) view.findViewById(R.id.tv_foodlist_price);
        }
    }
}
